package net;

import jdk.nashorn.internal.runtime.logging.DebugLogger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 聊天(Chat)室服务端
 */
public class ChatServer {
    //定义服务端的ServerSocket属性
    private ServerSocket serverSocket;
    public ChatServer()  {
        try {
            System.out.println("服务端开始启动");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务器启动OK");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("服务器启动失败");
            System.exit(-1);
        }
    }
    //启动服务读取数据
    public void start(){
        try {
            //1.接收客户端的连接
            Socket socket = serverSocket.accept();
            System.out.println("客户端连接成功");
            //2.构建输入流对象
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(), StandardCharsets.UTF_8));
            //3.通过此对象读取数据
            String data=null;
            while((data=br.readLine())!=null) {
                System.out.println("从客户端读取到的数据:" + data);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new ChatServer().start();
    }

}